#include "CaseDB/Impl/ValueConversions.hpp"
#include "CaseDB/DbTypes.hpp"
#include <cassert>

namespace CaseDB { namespace Impl
{

QVariant ValueConversions::anyToQVariant( DbTypes dbType, const boost::any& value)
{
	using namespace boost;
	switch( dbType)
	{
	case DbTypesEnum::Integer:
		return value.empty() ? QVariant( QVariant::Int) : any_cast< int>( value);
	case DbTypesEnum::Number:
		return value.empty() ? QVariant( QVariant::Double) : any_cast< double>( value);
	case DbTypesEnum::String:
		return value.empty() ? QVariant( QVariant::String) : QString::fromStdString( any_cast< std::string>( value));
	case DbTypesEnum::Blob:
		// TODO: Implement
	default:
		assert( false);
	}
	return QVariant();
}

// ---------------------------------------------------------------------------------------------------------------------

boost::any ValueConversions::QVariantToAny( DbTypes dbType, const QVariant& value )
{
	using namespace boost;
	if( !value.isValid())
	{
		return boost::any();
	}

	switch( dbType)
	{
	case DbTypesEnum::Integer:
		return boost::any( qvariant_cast< int>( value));
	case DbTypesEnum::Number:
		return boost::any( qvariant_cast< double>( value));
	case DbTypesEnum::String:
		return boost::any( std::string( qvariant_cast< QString>( value).toUtf8().data()));
	case DbTypesEnum::Blob:
		// TODO: Implement
	default:
		assert( false);
	}
	return boost::any();
}


}} // namespace Impl // namespace CaseDB
